% % ADMM-based OFDM Radar Waveform Design
% % Objective: Minimize spectral WISL via unimodular sequence
% % Constraints: Time-domain oversampled near-unimodularity, and unimodular constraint

close all;clear


%% Parameters
N = 128;                % Sequence length
M = 1*N;                % Oversampling factor
rho = 5e-1;             % ADMM penalty parameter
maxIter = 1000;
epsilon = 1e0;          % Time-domain unimodularity tolerance


%% Initialization
u = exp(1j*2*pi*rand(N,1));           % Initial ofdm weights
u_init = u;
IFM = (M / N) * dftmtx(M)' / M;
IFM = IFM(:,1:N);

tim_seq = ifft(u_init);

figure
plot(abs(tim_seq));

pos = 7;
acf = conv(tim_seq, conj(flip(tim_seq)));
figure(2);hold on;legend;
title("自相关")
plot(abs(acf),'DisplayName', "初始自相关");

tim_seq_append = zeros(2*N, 1);
tim_seq_append(1:N, 1) = tim_seq;
tim_seq_append_fft = fft(tim_seq_append);
tim_seq_append_fft_pow = (abs(tim_seq_append_fft)).^2;
plot(abs(ifft(tim_seq_append_fft_pow)),'DisplayName', "补零产生的自相关")


angleOfAcf = angle(acf(N- pos + 1));


clip_spec = zeros(2*N, 1);
clip_spec(1:N,1) = cos(pi / N * pos * (1:N));
acf_clip = ifft(clip_spec);

figure(3)
plot(abs(acf_clip))

clippedAcfAngleDiff = angleOfAcf / (pos-1);
clippedAcfAngle = zeros(2*N,1);
clippedAcfAngle(1:N) = flip(0:clippedAcfAngleDiff:clippedAcfAngleDiff*(N-1));
clippedAcfAngle(N:2*N-1) = -(0:clippedAcfAngleDiff:clippedAcfAngleDiff*(N-1));
acf_clip = acf_clip .* exp(1i * clippedAcfAngle);
clip_spec = fft(acf_clip);

phaOfU = angle(u_init);
powOfU = (abs(u_init)).^2;
powOfClippedU = powOfU - 0.1*clip_spec(1:2:2*N);
acfClippedU = sqrt(powOfClippedU) .* exp(1i * phaOfU);
clippedTimSeq = ifft(acfClippedU);
acfClipped = conv(clippedTimSeq, conj(flip(clippedTimSeq)));

figure(2);
p = plot(abs(acfClipped), 'DisplayName', "cliped自相关");















